package com.yunchen.server.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yunchen.server.mapper.DepartmentsMapper;
import com.yunchen.server.mapper.BaseDaoMapper;
import com.yunchen.server.pojo.Departments;
import com.yunchen.server.util.Return;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

@Service
public class DepartmentsService {
    @Autowired
    private BaseDaoMapper baseDaoMapper;
    @Autowired
    private DepartmentsMapper departmentsMapper;
    public Return Add( Departments departments) {
        Return re = new Return();
        departments.setParentId(Long.valueOf(0));
        departments.setCreatedAt(new Date());
        departmentsMapper.insertSelective(departments);
        re.setCode(200);
        re.setMsg("成功");
        return re;
    }

    public Return Update( Departments departments) {
        Return re = new Return();
        departmentsMapper.updateByPrimaryKeySelective(departments);
        re.setCode(200);
        re.setMsg("成功");
        return re;
    }
    public Return Del( Departments departments) {
        Return re = new Return();
        departmentsMapper.deleteByPrimaryKey(departments.getDepartmentId());
        re.setCode(200);
        re.setMsg("成功");
        return re;
    }

    public Integer Count(JSONObject json) {
        String department_name = json.getString("department_name");
        String tj="";
        if (department_name!=null){
            if (department_name!=""){ tj=tj+"  and  department_name like '%"+department_name+"%'  "; }
        }
        String SQL="SELECT  count(department_id) as mun  from departments where   parent_id=0 "+tj+"   ";
        Integer integer = baseDaoMapper.selectCountSql(SQL);
        return integer;
    }
    public JSONArray List (JSONObject json) {
        Integer page = json.getInteger("page");
        Integer limit = json.getInteger("limit");
        String department_name = json.getString("department_name");
        String tj="";
        if (department_name!=null){
            if (department_name!=""){ tj=tj+"  and  department_name like '%"+department_name+"%'  "; }
        }
        String SQL="SELECT * FROM departments where  parent_id=0  "+tj+"  ORDER BY sort_order asc LIMIT "+(page-1)*limit+","+limit+"  ";
//        System.out.println(SQL);
        List<LinkedHashMap<String, Object>> linkedHashMaps = baseDaoMapper.selectSql(SQL);
        JSONArray listArray = JSON.parseArray(JSON.toJSONString(linkedHashMaps));
        return listArray;
    }


    public JSONArray PullDown () {
        String SQL="SELECT * FROM departments where  parent_id=0  and status=1  ORDER BY sort_order asc   ";
        List<LinkedHashMap<String, Object>> linkedHashMaps = baseDaoMapper.selectSql(SQL);
        JSONArray listArray = JSON.parseArray(JSON.toJSONString(linkedHashMaps));
        return listArray;
    }



}
